package com.zjuee.web.ledger.controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.ledger.LedgerDisposeBillDto;
import com.zjuee.base.dto.common.ledger.LedgerDisposeGatherDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.ledger.LedgerDispose;
import com.zjuee.base.model.common.ledger.LedgerDisposeApproval;
import com.zjuee.base.model.common.ledger.LedgerDisposeBill;
import com.zjuee.base.model.common.ledger.LedgerDisposeGather;
import com.zjuee.service.LedgerDisposeApprovalService;
import com.zjuee.service.LedgerDisposeBillService;
import com.zjuee.service.LedgerDisposeGatherService;
import com.zjuee.service.LedgerDisposeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_LEDGER_URL_PREFIX)
public class DisposeLedgerController {
    @Autowired
    LedgerDisposeGatherService ledgerDisposeGatherService;
    @Autowired
    LedgerDisposeBillService ledgerDisposeBillService;
    @Autowired
    LedgerDisposeService ledgerDisposeService;
    @Autowired
    LedgerDisposeApprovalService ledgerDisposeApprovalService;

    @Resource
    LoginDto loginDto;
    @PostMapping("/dispose/add")
    public R<Long> add(@RequestBody LedgerDispose ledgerDispose) {
        try {
            ledgerDisposeService.add(ledgerDispose);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("填报失败!");
        }

        return R.ok("台账填报成功!");
    }


    @PostMapping("/dispose/isInitLedgerDispose")
    public R<Map> isInitLedgerDispose(@RequestBody LedgerDispose ledgerDispose) {
        Map<String, Object> initLedgerDispose =new HashMap<>();
        try {
            initLedgerDispose=ledgerDisposeService.isInitLedgerDispose(ledgerDispose);

        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok(initLedgerDispose);
    }
    //库存变更
    @PostMapping("/dispose/stock/change")
    public R<Long> stockChange(@RequestBody LedgerDisposeApproval ledgerDisposeApproval) {
        try {

            ledgerDisposeApprovalService.add(ledgerDisposeApproval);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    //查询处置台账汇总信息列表
    @PostMapping("/dispose/gather/list")
    public R<Page<LedgerDisposeGather>> gatherList(@RequestBody LedgerDisposeGatherDto ledgerDisposeGatherDto) {
        Page<LedgerDisposeGather> disposeGatherPage = new Page<>();
        try {
            ledgerDisposeGatherDto.setWldgMark(DictionaryConstants.WLD_MARK_COMMON);
            ledgerDisposeGatherDto.setBciId(loginDto.getOrgBusinessId());
            disposeGatherPage = ledgerDisposeGatherService.list(ledgerDisposeGatherDto);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok(disposeGatherPage);
    }
    //查询处置台账库存基本信息
    @PostMapping("/dispose/gather/getList")
    public R<List<LedgerDisposeGather>> getList(@RequestBody LedgerDisposeGather ledgerDisposeGather) {
        return R.ok(ledgerDisposeGatherService.getList(ledgerDisposeGather));
    }
    //查询处置台账明细信息列表
    @PostMapping("/dispose/bill/list")
    public R<Page<LedgerDisposeBill>> billList(@RequestBody LedgerDisposeBillDto ledgerDisposeBillDto) {
        Page<LedgerDisposeBill> list=new Page<>();
        try {
            ledgerDisposeBillDto.setBciId(loginDto.getOrgBusinessId());
            list= ledgerDisposeBillService.list(ledgerDisposeBillDto);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok(list);
    }
    //导出处置台账汇总信息
    @PostMapping("/dispose/gather/excel")
    public void disposeGatherExcel(@RequestBody LedgerDisposeGather ledgerDisposeGather) {
        ledgerDisposeGatherService.exportExcel(ledgerDisposeGather);
    }
    //导出处置台账汇总信息
    @PostMapping("/dispose/bill/excel")
    public void disposeBillExcel(@RequestBody LedgerDisposeBill ledgerDisposeBill) {
        ledgerDisposeBillService.exportBillExcel(ledgerDisposeBill);
    }
    //处置台账删除
    @GetMapping("/dispose/delete/gather/{id}")
    public R<Long> deleteGather(@PathVariable("id")  Long id) {
        try {
            ledgerDisposeGatherService.delete(id, loginDto.getOrgBusinessId());
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail();
        }
        return R.ok();
    }
}
